package com.xkdx.guangguang.fragment.user;

import java.util.HashMap;

import wealk.android.jewels.Jewels;
import wealk.android.jewels.JewelsHome;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.my.setup.AttentionFragment;
import com.xkdx.guangguang.fragment.my.setup.ExchangeListFragment;
import com.xkdx.guangguang.fragment.my.setup.ExchangeShopFragment;
import com.xkdx.guangguang.fragment.my.setup.PointFragment;
import com.xkdx.guangguang.fragment.my.setup.SetupFragment;
import com.xkdx.guangguang.fragment.myinfomation.InfoNotificationFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;
import com.xkdx.guangguang.zxing.CaptureActivity;

public class UserHomeFragment extends BaseFragment implements OnClickListener {
	private View view;
	private ImageButton user_mylogin, user_setup, point, game, exchange_shop,
			user_attention, use_have_exchange_goods, shop_manager;
	private RelativeLayout user_infos;
	private TextView red_tip_message;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.myinfos, container, false);
		findViews();
		return view;
	}

	@Override
	public void onStart() {
		if (Jewels.SUBMITANDINTENT == 1) {
			Jewels.SUBMITANDINTENT = 0;
			ExchangeShopFragment exchangeShopFragment = new ExchangeShopFragment();
			getFragmentManager().beginTransaction()
					.replace(R.id.fragment, exchangeShopFragment)
					.addToBackStack(null).commit();
		}
		super.onStart();
	}

	private void findViews() {
		IConstants.hideInputMethodAways(getActivity(), view);
		red_tip_message = (TextView) view
				.findViewById(R.id.my_message_tip_icon1);
		if ( IConstants.isFromPush) {
			red_tip_message.setVisibility(View.VISIBLE);
		} else {
			red_tip_message.setVisibility(View.GONE);
//			((UserActivity) getActivity()).setRedState();
		}
		user_mylogin = (ImageButton) view.findViewById(R.id.user_mylogin);
		user_setup = (ImageButton) view.findViewById(R.id.user_setup);
		user_infos = (RelativeLayout) view.findViewById(R.id.user_infos);
		point = (ImageButton) view.findViewById(R.id.user_point);
		game = (ImageButton) view.findViewById(R.id.user_im_game);
		exchange_shop = (ImageButton) view.findViewById(R.id.user_exchange);
		user_attention = (ImageButton) view.findViewById(R.id.user_myattention);
		use_have_exchange_goods = (ImageButton) view
				.findViewById(R.id.use_have_exchange_goods);
		shop_manager = (ImageButton) view
				.findViewById(R.id.shop_manager_check_or);

		exchange_shop.setOnClickListener(this);
		game.setOnClickListener(this);
		point.setOnClickListener(this);
		user_mylogin.setOnClickListener(this);
		user_setup.setOnClickListener(this);
		user_attention.setOnClickListener(this);
		use_have_exchange_goods.setOnClickListener(this);
		shop_manager.setOnClickListener(this);
		user_infos.setOnClickListener(this);

		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(),
				IConstants.SP_USER);
		String type = su.getVerifyType();

		if (type.equals("2") || type.equals("3")) {
			shop_manager.setVisibility(View.VISIBLE);
			shop_manager.setClickable(true);
		}
	}
	

	@Override
	public void onClick(View v) {
		UserSharePrefence sp_user = new UserSharePrefence(getActivity(),
				IConstants.SP_USER);
		String loginToken = sp_user.getUserLoginToken();
		switch (v.getId()) {
		case R.id.user_mylogin: // 跳转到登陆页面
			if ("".equals(loginToken)) {
				android.support.v4.app.FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
				UserLoginFragment userLoginFragment = new UserLoginFragment();
				ft.replace(R.id.fragment, userLoginFragment,
						"userLoginFragment");
				ft.addToBackStack(null);
				ft.commit();
			} else {
				FragmentTransaction ft1 = getFragmentManager()
						.beginTransaction();
				UserLoginSuccessFragment userLoginSuccessFragment = new UserLoginSuccessFragment();
				ft1.replace(R.id.fragment, userLoginSuccessFragment,
						"userLoginSuccessFragment");
				ft1.addToBackStack(null);
				ft1.commit();
			}
			break;

		case R.id.user_setup:
			SetupFragment setupFragment = new SetupFragment();
			getFragmentManager().beginTransaction()
					.replace(R.id.fragment, setupFragment).addToBackStack(null)
					.commit();
			break;
		case R.id.user_exchange:

			ExchangeShopFragment exchangeShopFragment = new ExchangeShopFragment();
			Bundle b = new Bundle();
			b.putString("loginToken", loginToken);
			exchangeShopFragment.setArguments(b);
			getFragmentManager().beginTransaction()
					.replace(R.id.fragment, exchangeShopFragment)
					.addToBackStack(null).commit();
			break;
		case R.id.user_infos:

			InfoNotificationFragment infoFragment = new InfoNotificationFragment();
			getFragmentManager()
					.beginTransaction()
					.replace(R.id.fragment, infoFragment,
							"infoNotificationFragment").addToBackStack(null)
					.commit();

			break;

		case R.id.user_point:
			if ("".equals(loginToken)) {
				SelfUserLogin();
			} else {
				PointFragment pointFragment = new PointFragment();
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, pointFragment)
						.addToBackStack(null).commit();
			}
			break;

		case R.id.shop_manager_check_or:
			Intent or_intent = new Intent(getActivity(), CaptureActivity.class);
			startActivity(or_intent);
			break;
		case R.id.user_im_game:
			Intent intent = new Intent(getActivity(), JewelsHome.class);
			startActivity(intent);
			break;
		case R.id.user_myattention:
			if ("".equals(loginToken)) {
				SelfUserLogin();
			} else {
				AttentionFragment attentionFragment = new AttentionFragment();
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, attentionFragment)
						.addToBackStack(null).commit();
			}
			break;
		case R.id.use_have_exchange_goods:
			if ("".equals(loginToken)) {
				SelfUserLogin();
			} else {
				ExchangeListFragment exchangeListFragment = new ExchangeListFragment();
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, exchangeListFragment)
						.addToBackStack(null).commit();
			}

			break;
		default:
			break;
		}
	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOnClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

}
